package com.fastadmin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fastadmin.mapper.impl.RoleMapper;
import com.fastadmin.mapper.impl.UserMapper;
import com.fastadmin.mapper.impl.UserRoleMapper;
import com.fastadmin.pojo.FastParam;
import com.fastadmin.pojo.Role;
import com.fastadmin.pojo.User;
import com.fastadmin.pojo.UserRole;
import com.fastadmin.service.UserService;
import com.google.common.collect.ImmutableMap;
import com.sun.jna.platform.win32.WinDef;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequiredArgsConstructor
public class UserController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    private final UserMapper userMapper;
    private final UserRoleMapper userRoleMapper;
    private final RoleMapper roleMapper;
    private final UserService userService;

    @GetMapping("/sys/user/index")
    public String userIndex() {
        return "sys/user/index";
    }

    @GetMapping("/sys/user/add")
    public String userAdd() {
        return "sys/user/add";
    }

    @GetMapping("/sys/user/edit")
    public String userEdit(String ids, Model model) {
        User user = userMapper.findById(ids);
        model.addAttribute("user", user);
        return "sys/user/edit";
    }

    @RequestMapping("/sys/user/save")
    @ResponseBody
    public String userSave(User user) {
        int code = -1;
        //id 不是null
        if (ObjectUtil.isNotNull(user.getId())) {
            //修改
            code = userMapper.updateByPrimaryKeySelective(user);
        } else {
            Object key = userMapper.saveAndGetKey(user);
            code = Integer.valueOf(key.toString());
        }
        return buildJson(1, "OK");
    }

    @RequestMapping("/sys/user/del")
    @ResponseBody
    public Object userDel(String ids) {
        //删除之前也要删除他在用户-角色表里面的数据，
        String[] split = ids.split(",");
        for (String s : split) {
            userService.deleteUser(s);
        }
        return buildJson(1, "OK");
    }

    @GetMapping("/sys/user/data")
    @ResponseBody
    public Object userData(FastParam fastParam) {
        PageInfo<User> pageInfo = userMapper.findByFastParam(fastParam);
        return ImmutableMap.of("rows", pageInfo.list, "total", pageInfo.total);
    }

    @GetMapping("/sys/user/granklist")
    public String granklist(@RequestParam String id, Model model) {
        List<Role> roles = roleMapper.findAll();
        List<UserRole> userRoles = userRoleMapper.findByUserId(id);
        for (Role role : roles) {
            for (UserRole userRole : userRoles) {
                if (role.getId() == userRole.getRole_id()) {
                    role.setFlag(true);
                }
            }
        }
        model.addAttribute("user_id", id);
        model.addAttribute("roles", roles);
        return "sys/user/granklist";
    }

    @RequestMapping("sys/user/grank")
    @ResponseBody
    public Object grank(@RequestParam Long user_id, @RequestParam List<Long> role_ids) {
        //查出用户的所有角色，多退少补
        ArrayList<UserRole> userRoles = CollUtil.newArrayList();
        int i = userRoleMapper.deleteByUserId(user_id);
        for (Long role_id : role_ids) {
            UserRole userRole = new UserRole().setRole_id(role_id).setUser_id(user_id);
            userRoles.add(userRole);
        }
        userRoleMapper.batchSave(userRoles);
        return buildJson(1, "ok");
    }
}
